package com.enbatis.mybatisplugs.service.impl;

import com.enbatis.mybatisplugs.base.Account;
import com.enbatis.mybatisplugs.base.BaseServiceImpl;

import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.entity.LeaveApply;
import com.enbatis.mybatisplugs.mapper.LeaveMapper;
import com.enbatis.mybatisplugs.service.LeaveService;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 描述：
* @author wwd
* @date 2020-04-13 18:29:17
*/

@Service
public class LeaveServiceImpl extends BaseServiceImpl<LeaveMapper, LeaveApply> implements LeaveService {


    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private TaskService taskService;

    @Override
    public ResultReturn addDraft(LeaveApply leave, Account account) {
        if (StringUtils.isBlank(leave.getName())){
            return  ResultReturn.fail("请填写标题");
        }

        if (StringUtils.isBlank(leave.getDetail())){
            return  ResultReturn.fail("请填写请假事由");
        }

        if (null == leave.getStartDate()){
            return  ResultReturn.fail("请填写请假开始时间");
        }

        if (null == leave.getEndDate()){
            return  ResultReturn.fail("请填写请假结束时间");
        }
        leave.setStatus(0);
        return ResultReturn.success(null!=insert(leave));
    }




    @Override
    public ResultReturn addSubmit(LeaveApply leave, Account account) {
        if (StringUtils.isBlank(leave.getName())){
          return   ResultReturn.fail("请填写标题");
        }

        if (StringUtils.isBlank(leave.getDetail())){
            return  ResultReturn.fail("请填写请假事由");
        }

        if (null == leave.getStartDate()){
            return ResultReturn.fail("请填写请假开始时间");
        }

        if (null == leave.getEndDate()){
            return  ResultReturn.fail("请填写请假结束时间");
        }
        leave.setStatus(1);
        leave.setApplyId(account.getId());
        leave.setApplyName(account.getUsername());
        leave.setApplyTime(new Date());
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String,Object> map =new HashMap<>();
        map.put("applyReason",leave.getDetail());
       if (null!=insert(leave)){
           ProcessInstance processInstance= runtimeService.startProcessInstanceById("leaveApply",leave.getId().toString(),map);
           return ResultReturn.success(null!=processInstance);
       }
        return ResultReturn.success(false);
    }

    @Override
    public ResultReturn submitProcess(Long[] ids, Account account) {
        if (ArrayUtils.isEmpty(ids)){
          return   ResultReturn.fail("请选择要提交的数据");
        }
        List<LeaveApply> leaveApplyList = super.list(new Wrapper<>(new LeaveApply()).in("id",ids));
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String,Object> map =new HashMap<>();

        IdentityService identityService = processEngine.getIdentityService();//
        for (LeaveApply leave:leaveApplyList) {
            leave.setStatus(1);
            leave.setApplyId(account.getId());
            leave.setApplyName(account.getUsername());
            leave.setApplyTime(new Date());
            map.put("applyReason",leave.getDetail());
            identityService.setAuthenticatedUserId( account.getId().toString());
            ProcessInstance processInstance= runtimeService.startProcessInstanceByKey("leaveApply",leave.getId().toString(),map);
            if (null!=processInstance){
                super.updateById(leave);
            }
        }

        return ResultReturn.success(true);
    }

    @Override
    public ResultReturn pass(String taskId,Long businessId, String opinion, Account account) {
        //获取流程实例

        if (StringUtils.isBlank(taskId)){
            return  ResultReturn.fail("任务ID不能为空");
        }
        if (StringUtils.isBlank(opinion)){
            return  ResultReturn.fail("审批意见不能为空");
        }

        if (null == businessId){
            return  ResultReturn.fail("业务ID不能为空");
        }
        LeaveApply leaveApply = getById(businessId);
        if (leaveApply!=null){
            if (StringUtils.isBlank(leaveApply.getOpinion())){
                leaveApply.setOpinion(opinion);
            }else{
                leaveApply.setOpinion(leaveApply.getOpinion()+" 人事审批意见："+opinion);
            }
            leaveApply.setStatus(2);
            leaveApply.setCheckDate(new Date());
            leaveApply.setChecker(account.getUsername());
            leaveApply.setCheckerId(account.getId());
        }

        if (updateById(leaveApply)>0){
            taskService.claim(taskId, account.getId().toString());
            Map<String,Object> vars = new HashMap<String,Object>();
            vars.put("leavePass", "true");
            taskService.complete(taskId, vars);
          return   ResultReturn.success(true);
        }
       return ResultReturn.success(false);
    }



    @Override
    public ResultReturn back(String taskId,Long businessId, String opinion, Account account) {
        //获取流程实例

        if (StringUtils.isBlank(taskId)){
            return  ResultReturn.fail("任务ID不能为空");
        }
        if (StringUtils.isBlank(opinion)){
            return  ResultReturn.fail("审批意见不能为空");
        }

        if (null == businessId){
            return  ResultReturn.fail("业务ID不能为空");
        }
        LeaveApply leaveApply = getById(businessId);
        if (leaveApply!=null){
            if (StringUtils.isBlank(leaveApply.getOpinion())){
                leaveApply.setOpinion(opinion);
            }else{
                leaveApply.setOpinion(leaveApply.getOpinion()+" 人事审批意见："+opinion);
            }
            leaveApply.setStatus(3);
            leaveApply.setCheckDate(new Date());
            leaveApply.setChecker(account.getUsername());
            leaveApply.setCheckerId(account.getId());
        }

        if (updateById(leaveApply)>0){
            taskService.claim(taskId, account.getId().toString());
            Map<String,Object> vars = new HashMap<>();
            vars.put("leavePass", "false");
            taskService.complete(taskId, vars);
            return   ResultReturn.success(true);
        }
        return ResultReturn.success(false);
    }
}
